package com.blueocean_health.care.management.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.ChargingRules;
import com.blueocean_health.care.management.domain.base.OrderRefundInfo;
import com.blueocean_health.care.management.domain.base.PostageV;
import com.blueocean_health.care.management.domain.dto.OrderSelectInfoEntity;
import com.blueocean_health.care.management.domain.ordervo.PostageVo;
import com.blueocean_health.care.management.em.ServiceTypeEnum;
import com.blueocean_health.care.management.mapper.ChargingRulesMapper;
import com.blueocean_health.care.management.mapper.PostageVMapper;
import com.blueocean_health.care.management.service.PostageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
public class PostageServiceImpl implements PostageService{
	@Resource
	private PostageVMapper postageVMapper;
	@Resource
	private ChargingRulesMapper chargingRulesMapper;
	/**
	 * 查询资费管理列表
	 */
	@Override
	public PageInfo<PostageV>  selectListPostage(PostageVo postageVVo){
		PostageV postageV=new PostageV();
		BeanUtils.copyProperties(postageVVo, postageV);
		PageHelper.startPage(Integer.valueOf(StringUtils.isEmpty(postageVVo.getPage())?"1":postageVVo.getPage()),Integer.valueOf(StringUtils.isEmpty(postageVVo.getPageSize())?"65536":postageVVo.getPageSize()));
//		List<PostageV> selectList = postageVMapper.select(postageV);
		List<PostageV> selectForlist = postageVMapper.selectForlist(postageV);
		PageInfo<PostageV> pageInfo = new PageInfo<PostageV>(selectForlist);
		return pageInfo;
		
	}
	/**
	 * 导出资费列表
	 */
	@Override
	public List<PostageV>  selectListPostageExcel(PostageVo postageVVo){
		PostageV postageV=new PostageV();
		BeanUtils.copyProperties(postageVVo, postageV);
		List<PostageV> selectList = postageVMapper.selectForlist(postageV);
		return selectList;
		
	}
	/**
	 * 新增资费信息
	 */
	@Override
	 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int  addPostage(PostageVo postageVVo){
		
		ChargingRules chargingRules=new ChargingRules();
		ChargingRules chargingRulescheck=new ChargingRules();
		chargingRulescheck.setHospitalId(postageVVo.getHospitalId());
		chargingRulescheck.setIllnessId(postageVVo.getIllnessId());
		chargingRulescheck.setDeptId(postageVVo.getDeptId());
		chargingRulescheck.setServiceId(postageVVo.getServiceId());
		chargingRulescheck.setSingleServiceId(postageVVo.getSingleServiceId());
		chargingRulescheck.setTimetypeId(postageVVo.getTimetypeId());
		chargingRulescheck.setCancel("0");
		//检查是否重复
		List<ChargingRules> select = chargingRulesMapper.select(chargingRulescheck);
		if(null!=select&&select.size()>0){
			return -1;
		}
		BeanUtils.copyProperties(postageVVo, chargingRules);
		if(postageVVo.getServiceId().equals(ServiceTypeEnum._5.getVal())){
			chargingRules.setNurseHourPay(0.0);
			chargingRules.setOneHourPay(0.0);
		}
		int insertSelective = chargingRulesMapper.insertSelective(chargingRules);
		return insertSelective;
	}
	/**
	 * 修改前查看资费信息
	 */
	@Override
	public PostageV queryPostage(Integer id){
		PostageV postageV=new PostageV();
		postageV.setId(id);
		PostageV selectByPrimaryKey = postageVMapper.getpostageVbyid(id);
		return selectByPrimaryKey;
		
	}
	/**
	 * 修改资费信息
	 */
	@Override
	 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int updatePostage(PostageVo postageVVo){
		ChargingRules chargingRules=new ChargingRules();
		BeanUtils.copyProperties(postageVVo, chargingRules);
		int updateByPrimaryKeySelective = chargingRulesMapper.updateByPrimaryKeySelective(chargingRules);
		return updateByPrimaryKeySelective;
		
	}
	/**
	 * 删除资费信息
	 */
	@Override
	 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int deletePostage(PostageVo postageVVo){
		ChargingRules chargingRules=new ChargingRules();
		Integer id = postageVVo.getId();
		chargingRules.setId(id);
		chargingRules.setCancel("1");
		int updateByPrimaryKeySelective = chargingRulesMapper.updateByPrimaryKeySelective(chargingRules);
		return updateByPrimaryKeySelective;
	}
	/**
	 * 判断该科室是否有数据
	 */
	@Override
	public boolean checkdeptId(String  deptId){
		ChargingRules chargingRules=new ChargingRules();
		chargingRules.setDeptId(deptId);
		chargingRules.setCancel("0");
		chargingRulesMapper.selectCount(chargingRules);
		
		//true为有数据  
		return chargingRulesMapper.selectCount(chargingRules)>0;
	}
	/**
	 * 根据科室判断资费信息
	 */
	@Override
	public boolean deletebydept(String  deptId){
		ChargingRules chargingRules=new ChargingRules();
		chargingRules.setCancel("1");
		Example example = new Example(ChargingRules.class);
		example.createCriteria().andEqualTo("deptId",deptId);
		int updateByExampleSelective = chargingRulesMapper.updateByExampleSelective(chargingRules, example);
		//true为没数据  
		return updateByExampleSelective>0;
	}
	
	
}
